package com.sopaths.crawler.zhilian;

import java.util.LinkedList;
import java.util.List;

import javax.inject.Named;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sopaths.contract.crawler.City;
import com.sopaths.crawler.CityCrawler;
import com.sopaths.crawler.tasks.CityCrawlerTask;
import com.sopaths.crawler.tasks.CrawlerTask;

@Named
public class ZhiLianCityCrawler extends CityCrawler {

	private Logger logger = Logger.getLogger(ZhiLianCityCrawler.class);

	@Override
	protected String assembleURL(CrawlerTask<?> task) {
		return "http://www.zhaopin.com/citymap.html";
	}

	@Override
	protected void assembleCityCrawlerTaskResult(CityCrawlerTask task, Document document) {
		logger.info("Assembling city crawler task result.");
		Elements elements = document.select(".infor dd a:not(.infor-more)");
		List<City> cities = new LinkedList<>();
		for (Element element : elements) {
			String cityName = element.text();
			if (cityName == null) {
				logger.warn("Unexpected empty element text: " + element);
				continue;
			}
			cityName = cityName.trim();
			logger.trace("City Name: " + cityName);
			City city = new City();
			city.setName(cityName);
			city.setCode(cityName);
			cities.add(city);
		}
		task.setResult(cities);
		logger.debug("Assemble city crawler task result successfully. City count: " + cities.size());
	}
/*
	@Override
	public void crawl(CrawlerTask<?> task) {
		try {
			logger.info("Crawl city information from zhilian.com");
			CityCrawlerTask innerTask = (CityCrawlerTask) task;
			HttpGet httpGet = new HttpGet("http://www.zhaopin.com/citymap.html");
			try (CloseableHttpResponse response = getHttpClient().execute(httpGet)) {
				HttpEntity entity = response.getEntity();
				String html = EntityUtils.toString(entity, Charset.forName("UTF-8"));
				Document document = Jsoup.parse(html);
				Elements elements = document.select(".infor dd a:not(.infor-more)");
				List<City> cities = new LinkedList<>();
				for (Element element : elements) {
					City city = new City();
					city.setName(element.text().trim());
					cities.add(city);
				}
				innerTask.setResult(cities);
			}
		} catch (Exception ex) {
			logger.error("An error occurred while crawling.", ex);
		}
	}
*/
	public static void main(String[] args) {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://www.baidu.com");
		try (CloseableHttpResponse response = client.execute(httpGet)) {
			HttpEntity entity = response.getEntity();
			String html = EntityUtils.toString(entity);
			Document document = Jsoup.parse(html);
			Elements elements = document.select("body");
			for (Element element : elements) {
				System.out.println(element.text());
			}
			System.out.println(html);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
